﻿using System;
using System.Diagnostics; // Process Class
using System.Windows;

namespace AFT
{
    /* Device Information Loader
     * Build Version
     * Bootloader
     * Radio ?? 먼지 모르겠음
     * Network - 통신사
     * Kernel 
     * Model Name
     * Serial
     * 
     * 
     * 
     */
    class DeviceInformationLoader
    {
        string buildVersion;
        string bootLoader;
        string radio;
        string network;
        string kernelVersion;
        string uptime;
        string modelName;
        string serial;

        ~DeviceInformationLoader()
        {

        }

        public string getModelName()
        {
            return modelName;
        }

        public string getSerial()
        {
            return serial;
        }

        public string getBuildVersion()
        {
            return buildVersion;
        }

        public string getBootLoader()
        {
            return bootLoader;
        }

        public string getRadio()
        {
            return radio;
        }

        public string getNetwork()
        {
            return network;
        }

        public string getKernelVersion()
        {
            return kernelVersion;
        }
        public string getUptime()
        {
            return uptime;
        }

        private void cmdProcess_Exited(object sender, System.EventArgs e)
        {
            //가져오기 완료하면 이벤트 발생
        }

        private void connected_ADB(object sender, System.EventArgs e)
        {
            //MessageBox.Show("Run ADB.");
        }

        public int getDeviceInformation()
        {
            if (getBasicInformation() == -1)
                return -1;
            
            return 1;
        }

        private int getCpuInformation()
        {
            return 1;
        }

        public int runADB()
        {
            Process process = new Process();

            process.StartInfo.WorkingDirectory = "C:\\Program Files\\Android\\android-sdk\\platform-tools";
            process.StartInfo.FileName = "adb.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            process.StartInfo.CreateNoWindow = true;
            process.EnableRaisingEvents = true;
            process.StartInfo.Arguments = "device";
            process.Exited += new EventHandler(connected_ADB);

            if (process.Start())
                return 1;
            else
                return 0;

            /*
             * ADB 실행되어있는지 체크해야됨. 
             */
        }

        private Process runADB(string directory, string filename, string argument)
        {
            Process process = new Process();
            process.StartInfo.WorkingDirectory = directory;
            process.StartInfo.FileName = filename;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.Arguments = argument;
            process.EnableRaisingEvents = true;
            process.Exited += new EventHandler(cmdProcess_Exited);
            process.Start();
            return process;
        }

        private int getBasicInformation()
        {
            /*
            Process process = new Process();
            process.StartInfo.WorkingDirectory = "C:\\Program Files\\Android\\android-sdk\\platform-tools";
            process.StartInfo.FileName = "adb.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.Arguments = "bugreport";
            process.EnableRaisingEvents = true;
            process.Exited += new EventHandler(cmdProcess_Exited);
            process.Start();
            */



            Process process = runADB("C:\\Program Files\\Android\\android-sdk\\platform-tools", "adb.exe", "bugreport");
            string[] outputStream = new string[25];
            for (int i = 0; i < 25; i++)
            {
                outputStream[i] = process.StandardOutput.ReadLine();
            }

            process.Kill();

            if (outputStream[0] == null)
            {
                return -1;
            }
            else
            {
                buildVersion = outputStream[8].Substring(7);
                bootLoader = outputStream[10].Substring(12);
                radio = outputStream[12].Substring(7);
                network = outputStream[14].Substring(9);
                kernelVersion = outputStream[16].Substring(8);
                uptime = outputStream[24].Substring(9, 8);
            }



            process = runADB("C:\\Program Files\\Android\\android-sdk\\platform-tools", "adb.exe", "shell cat /proc/cpuinfo");
            outputStream = new string[1];
            
            outputStream[0] = process.StandardOutput.ReadToEnd();
            
            if (outputStream[0] == null)
            {
                return -1;
            }
            else
            {
                int start = outputStream[0].IndexOf("Hardware\t: ") + 11;
                modelName = outputStream[0].Substring(start);
                int end = modelName.IndexOf("\r\r\nRevision\t:");
                modelName = modelName.Remove(end);

                start = outputStream[0].IndexOf("Serial\t\t: ") + 10;
                serial = outputStream[0].Substring(start);
                end = serial.IndexOf("\r\r\n");
                serial = serial.Remove(end);
            }




            return 1;


        }

    }
}
